<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
	<!-- <meta name="viewport" content="user-scalable=no,viewport-fit=cover" /> -->
	<meta name="viewport" content="initial-scale=1.0, user-scalable=no"> 
	<link href="./css/baseCss.css" rel="stylesheet" type="text/css"/>
	<link href="./base/layer.css" rel="stylesheet" type="text/css"/>
	<!-- <link href="./base/articlecon.css" rel="stylesheet" type="text/css"/> -->
	<link href="./base/base2.css" rel="stylesheet" type="text/css"/>
	<script type="text/javascript" src="./base/rem.js"></script>
	<script type="text/javascript" src="./base/iscroll.js"></script>
	<style type="text/css">
		body,html{
			width: 100%;
			height: 90%;
		}
		#container{
			width: 100%;
			height: 100%;
		}
		#myPageTop{
			/*position: absolute;
    		top: 0;*/
            width: 100%;
            height: 20%;
		}
		#panel{
			position: absolute;
            width: 300px;
            bottom: 0;
            width: 100%;
            z-index: 200;
		}
        #lnglat{
            width: 2rem;
            height: 2rem;
            background-color: #fff;
            border-radius: 2rem;
            position: absolute;
            top: 0;
        }
	</style>
	<title>选择地址</title>
</head>

<body>
	<div id="container"></div>
	<div id="panel"></div>
	<div id="myPageTop">
		<span id="lnglat" onclick="disSaveAdd();"></span>
	</div>
	
</body>
<script type="text/javascript" src="./js/jquery.min.js"></script>
<script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.8&key=b92b3107759eeb83902ab0c7a654e95b"></script> 
<script type="text/javascript">
    getlocation();
    // dot(116.39,39.9);
   	
   	// 定位到当前位置
    function getlocation(){
        map = new AMap.Map('container');
        map.plugin('AMap.Geolocation', function () {

            geolocation = new AMap.Geolocation({
                enableHighAccuracy: false,//是否使用高精度定位，默认:true
                timeout: 10000,          //超过10秒后停止定位，默认：无穷大
                maximumAge: 0,           //定位结果缓存0毫秒，默认：0
                convert: true,           //自动偏移坐标，偏移后的坐标为高德坐标，默认：true
                showButton: true,        //显示定位按钮，默认：true
                buttonPosition: 'LB',    //定位按钮停靠位置，默认：'LB'，左下角
                buttonOffset: new AMap.Pixel(10, 20),//定位按钮与设置的停靠位置的偏移量，默认：Pixel(10, 20)
                showMarker: true,        //定位成功后在定位到的位置显示点标记，默认：true
                showCircle: true,        //定位成功后用圆圈表示定位精度范围，默认：true
                panToLocation: true,     //定位成功后将定位到的位置作为地图中心点，默认：true
                zoomToAccuracy:true      //定位成功后调整地图视野范围使定位位置及精度范围视野内可见，默认：false
            });

            map.addControl(geolocation);
            //geolocation.getCurrentPosition();
            //AMap.event.addListener(geolocation, 'complete', onComplete);//返回定位信息
            //AMap.event.addListener(geolocation, 'error', onError);      //返回定位出错信息
        });

       


        geolocation.getCurrentPosition(function(status,result){
                console.log(result);
                var city = result.addressComponent.city;//获取地理位置信息
                // $("#city").text(city.substring(0,city.length-1));
                var province = result.addressComponent.province;
                localStorage.setItem("province",province);
                localStorage.setItem("city",city);
        });

        //拖曳地图结束事件
        map.on("moveend",function(){
    
            var currentCenter = map.getCenter();
    
            $(".amap-icon").remove();
            dot(currentCenter.lng,currentCenter.lat);
    
            
            search(currentCenter.lng,currentCenter.lat);
            
            
        });
    }

    //周边查询
    function search(lng,lat){
        
        AMap.service(["AMap.PlaceSearch"], function() {
            var placeSearch = new AMap.PlaceSearch({ //构造地点查询类
                pageSize: 5,
                type: '汽车服务|汽车销售|汽车维修|摩托车服务|餐饮服务|购物服务|生活服务|体育休闲服务|医疗保健服务|住宿服务|风景名胜|商务住宅|政府机构及社会团体|科教文化服务|交通设施服务|金融保险服务|公司企业|道路附属设施|地名地址信息|公共设施',
                pageIndex: 1,
                map: map,
                panel: "panel",
                showCover: false,
                map: "",
            });
            var cpoint = [lng,lat]; //中心点坐标
            
            

            placeSearch.searchNearBy('地名地址信息', cpoint, 5000000, function(status, result) {
                if(status == "complete"){
                    for(var i=0;i<$(".poibox").length;i++){
                        $(".poibox").eq(i).attr("data-lat",result.poiList.pois[i].location.lat);
                        $(".poibox").eq(i).attr("data-lng",result.poiList.pois[i].location.lng);
                    }
                }else if(status == "no_data"){
                    // alert("在当前地区查找不到地址");
                }else{
                    alert("查询错误");
                }
            });

            placeSearch.on("listElementClick",function(){
                var sel = $(".poibox.selected").eq(0);
                
                var lat = sel.attr("data-lat");
                var lng = sel.attr("data-lng");
                var district = sel.find(".poi-addr").text();

                district = district.substring(3,district.length);
                
                var address= sel.find(".poi-name").eq(0).text();
                localStorage.setItem("lat",lat);
                localStorage.setItem("lng",lng);
                localStorage.setItem("address",address);
                
                localStorage.setItem("district",district);
                
                // alert("纬度:"+sel.attr("data-lat")+"    经度:"+sel.attr("data-lng"));
                
                
                disSaveAdd();
            });

            placeSearch.clear();
        });
    }
   	

   	

   		//点标记
   	function dot(lng,lat){

   		// 创建一个 Marker 实例：
		var marker = new AMap.Marker({
		    position: new AMap.LngLat(lng, lat),   // 经纬度对象，也可以是经纬度构成的一维数组[116.39, 39.9]
		    title: '北京',		    
		});
        
		// 移除已创建的 marker
        
        map.remove(marker);
        
		

		// 将创建的点标记添加到已有的地图实例：
		map.add(marker);

   	}


   	 //获取url中"?"符后的字串
    function GetRequest() {
        var url = location.search; //获取url中"?"符后的字串   
        
        var theRequest = new Object();
        if (url.indexOf("?") != -1) {
          var str = url.substr(1);
          
          strs = str.split("&");
          
          for (var i = 0; i < strs.length; i++) {
            
            theRequest[strs[i].split("=")[0]] = strs[i]
                .split("=")[1];
          }
        }
        return theRequest;
    }

    //点击跳转到选择位置界面并传递保存数据
    function disSaveAdd(){
        var Request = new Object();
        Request = GetRequest();
        var phone = Request['phone'];
        var consignee = decodeURI(Request['consignee']);
        var addressId = decodeURI(Request['addressId']);
        
        var houseNumber = decodeURI(Request['houseNumber']);
        var tag = Request['tag'];
        var sex = Request['sex'];

        if(addressId=='undefined'){
            
            window.location.href='./addressNew.html?phone='+phone+'&consignee='+consignee+'&sex='+sex+'&tag='+tag+'&houseNumber='+houseNumber;
        }else{
            window.location.href='./addressNew.html?phone='+phone+'&consignee='+consignee+'&addressId='+addressId+'&sex='+sex+'&tag='+tag+'&houseNumber='+houseNumber;
        }
        
    }
</script>
</html>